﻿namespace Hidistro.UI.AccountCenter.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core.Enums;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Web.UI.WebControls;
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Sales;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Member;
    using Hidistro.UI.SaleSystem.Tags;
    using System.Collections.Specialized;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Data;
    using Hidistro.SaleSystem.Catalog;
    using System.Collections.Generic;
    using wnxd.Web;
    using Hidistro.Entities.Commodities;
    public class UserCenterDefault : AscxTemplatedWebControl
    {
        private Repeater rptOrderlist;
        private Repeater rptFavorites;
        private Repeater rptBrowseRecord;
        private int dscount = 0;
        protected override void AttachChildControls()
        {
            PageTitle.AddSiteNameTitle("用户中心");
            if (!this.Page.IsPostBack)
            {
                Member user = Users.GetUser(HiContext.Current.User.UserId, false) as Member;
                this.rptOrderlist = (Repeater)this.FindControl("rptOrderlist");
                this.rptOrderlist.ItemDataBound += rptOrderlist_ItemDataBound;
                this.rptFavorites = (Repeater)this.FindControl("rptFavorites");
                this.rptBrowseRecord = (Repeater)this.FindControl("rptBrowseRecord");
                
                BindOrders();
                BindFavorites();
                BindHistory();
            }
        }
        private void rptOrderlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater GList = (Repeater)e.Item.FindControl("rptOrderlistSon");
                GList.ItemDataBound += rptOrderlistson_ItemDataBound;
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                DataSet ds = TradeHelper.GetOrderGoodsNew(rowv["OrderId"].ToString());
                if (ds != null&&ds.Tables[0].Rows.Count>0)
                {
                    dscount = ds.Tables[0].Rows.Count;
                    GList.DataSource = ds;
                    GList.DataBind();
                    
                }
                HtmlAnchor anchor2 = (HtmlAnchor)e.Item.FindControl("hlinkPay");
                OrderStatus status = (OrderStatus)DataBinder.Eval(e.Item.DataItem, "OrderStatus");
                anchor2.Visible = status == OrderStatus.WaitBuyerPay;
            }
        }
        private void rptOrderlistson_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项 
                HyperLink aname = (HyperLink)e.Item.FindControl("aname");
                aname.Text = rowv["ProductName"].ToString();
                if (dscount == 1)
                {
                    aname.Visible = true;
                }
                else
                {
                    aname.Visible = false;
                }
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_UserCenter/Skin-UserCenterDefault.ascx";
            }
            base.OnInit(e);
        }
        private void BindOrders()
        {
            OrderQuery orderQuery = this.GetOrderQuery();
            DbQueryResult userOrder = TradeHelper.GetUserOrder(HiContext.Current.User.UserId, orderQuery);
            rptOrderlist.DataSource = userOrder.Data;
            rptOrderlist.DataBind();
        }

        private void BindFavorites()
        {
            Pagination page = new Pagination
            {
                PageIndex = 1,
                PageSize = 5
            };
            string tags = string.Empty;
            DbQueryResult favorites = ProductBrowser.GetFavorites(tags, page);
            this.rptFavorites.DataSource = favorites.Data;
            this.rptFavorites.DataBind();
        }
        private void BindHistory()
        {
            IList<int> browedProductList = BrowsedProductQueue.GetBrowedProductList();
            if (browedProductList.Count != 0)
            {
                rptBrowseRecord.DataSource = ProductBrowser.GetVistiedProducts(browedProductList);
                rptBrowseRecord.DataBind();
            }
        }

        private OrderQuery GetOrderQuery()
        {
            OrderQuery query = new OrderQuery();
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["orderId"]))
            {
                query.OrderId = this.Page.Request.QueryString["orderId"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["shipId"]))
            {
                query.ShipId = this.Page.Request.QueryString["shipId"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["shipTo"]))
            {
                query.ShipTo = this.Page.Request.QueryString["shipTo"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["orderId"]))
            { 
                query.OrderId = this.Page.Request.QueryString["orderId"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["startDate"]))
            {
                query.StartDate = new DateTime?(Convert.ToDateTime(this.Page.Server.UrlDecode(this.Page.Request.QueryString["startDate"])));
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["endDate"]))
            {
                query.EndDate = new DateTime?(Convert.ToDateTime(this.Page.Server.UrlDecode(this.Page.Request.QueryString["endDate"])));
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["orderStatus"]))
            {
                int result = 0;
                if (int.TryParse(this.Page.Request.QueryString["orderStatus"], out result))
                {
                    query.Status = (OrderStatus)result;
                }
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["sortBy"]))
            {
                query.SortBy = this.Page.Request.QueryString["sortBy"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["sortOrder"]))
            {
                int num2 = 0;
                if (int.TryParse(this.Page.Request.QueryString["sortOrder"], out num2))
                {
                    query.SortOrder = (SortAction)num2;
                }
            }
            query.PageIndex = 1;// this.pager.PageIndex;
            query.PageSize = 4;// this.pager.PageSize;
            return query;
        }

    }
}
